package com.herohead.swt;




import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;

import com.herohead.Misc;
import com.herohead.redis.RedisKeys;

import java.awt.event.ActionListener;

import javax.swing.Timer;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.forms.widgets.FormToolkit;

import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.events.HelpEvent;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class cloneStats {

	protected static Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */

	static Jedis jd;
	static Jedis jdw;
	private static Text threadRunning;

	public static Timer displayTimer;
	private static int refreshVal = 10000;
	private static int refreshVal10secs = 10000;
	private static int refreshVal30secs = refreshVal10secs * 3;
	private static int refreshVal1min = refreshVal30secs * 2;
	private static int refreshVal5min = refreshVal1min * 5;
	private static int refreshVal10min = refreshVal1min * 10;
	private static int refreshVal30min = refreshVal10min * 3;
	
	private static Label lastUpdate;
	static ActionListener listener;
	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	private static Text threadQueued;
	private static Text threadRunningSize;
	private static Text threadQueuedSize;
	static ProgressBar progressBarQueued;
	static ProgressBar progressBarRunning;
	static ProgressBar progressBarThreadsRunningSize;
	static ProgressBar progressBarThreadsQueuedSize;
	static Button buttonThreadRunningTrend;

	static Map <Integer, Double> lastValues = new HashMap<Integer, Double>();
	private Label label_1;
	private static Table tableQueued;
	private org.eclipse.swt.widgets.TableColumn tblclmnNewColumn;
	private org.eclipse.swt.widgets.TableColumn tblclmnSize;
	private static Table tableRunning;
	private org.eclipse.swt.widgets.TableColumn tableColumn;
	private org.eclipse.swt.widgets.TableColumn tableColumn_1;
	private static Text textSSIDNotCloned;
	private static Text textGroupNotCloned;
	private static Button buttonThreadQueuedTrend;
	private static Button buttonThreadsRunningSizeTrend;
	private static Button buttonThreadsQueuedSizeTrend;
	private static Button buttonSSIDNotClonedTrend;
	private static Button buttonGroupsNotClonedTrend;
	private Label label_3;
	private Label label_5;
	private Label label_7;
	static List listGroupsNotCloned;
	static String version = "1.06";
	static boolean iconnized = false;

	public static Status StatusThreadRunning;
	public static Status StatusQueuedRunning;
	public static Status StatusThreadRunningSize;
	public static Status StatusQueuedRunningSize;
	public static StatusSmall StatusSSIDNotCloned;
	public static StatusSmall StatusGroupNotCloned;
	public static StatusSmall StatusClonesRunning;
	public static StatusSmall StatusSSIDNotClonedSize;
	
	public static List listSessions;
	private Text txtNumOfClones;
	private static String userName;

	private static boolean f1pressed = true;
	
	private static String sessionInfoString;

	static Runnable timer;
	private MenuItem mntm30Min;
	private MenuItem mntm10Secs;
	private Text textSSIDNotClonedSize;
	private Label lblSsidNotClonedSize;
	private Button buttonSSIDNotClonedSizeTrend;
	
	
	public static void main(String[] args ) {

		final Display display = new Display();
		//final Shell shell = new Shell(display);

		try {
			jd = new Jedis("172.18.38.14",6380);
			cloneStats window = new cloneStats();
			timer = new Runnable() {
				public void run() {
					updateFields();
					display.timerExec(refreshVal, this);
				}
			};
			display.timerExec(refreshVal, timer);
			
			jdw = new Jedis("172.18.38.14",6379);

			window.open();	

		} catch (org.eclipse.swt.SWTException e) {	
			System.out.println(e.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();

		lastUpdate = new Label(shell, SWT.RIGHT);
		lastUpdate.setBounds(377, 400, 142, 15);
		lastUpdate.setText("Last Refresh");

		threadQueued = new Text(shell, SWT.BORDER);
		threadQueued.setBounds(125, 34, 36, 18);
		formToolkit.adapt(threadQueued, true, true);

		progressBarThreadsRunningSize = new ProgressBar(shell, SWT.NONE);
		progressBarThreadsRunningSize.setBounds(436, 16, 69, 17);
		formToolkit.adapt(progressBarThreadsRunningSize, true, true);

		progressBarThreadsQueuedSize = new ProgressBar(shell, SWT.NONE);
		progressBarThreadsQueuedSize.setBounds(436, 39, 69, 17);
		formToolkit.adapt(progressBarThreadsQueuedSize, true, true);

		textSSIDNotCloned = new Text(shell, SWT.BORDER);
		textSSIDNotCloned.setBounds(159, 400, 49, 21);
		formToolkit.adapt(textSSIDNotCloned, true, true);

		textGroupNotCloned = new Text(shell, SWT.BORDER);
		textGroupNotCloned.setBounds(159, 421, 49, 21);
		formToolkit.adapt(textGroupNotCloned, true, true);

		buttonThreadsRunningSizeTrend = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.RIGHT);
		buttonThreadsRunningSizeTrend.setSelection(true);
		buttonThreadsRunningSizeTrend.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		buttonThreadsRunningSizeTrend.setBounds(506, 16, 13, 16);
		formToolkit.adapt(buttonThreadsRunningSizeTrend, true, true);

		buttonThreadsQueuedSizeTrend = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.RIGHT);
		buttonThreadsQueuedSizeTrend.setSelection(true);
		buttonThreadsQueuedSizeTrend.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		buttonThreadsQueuedSizeTrend.setBounds(507, 40, 13, 16);
		formToolkit.adapt(buttonThreadsQueuedSizeTrend, true, true);

		buttonSSIDNotClonedTrend = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.RIGHT);
		buttonSSIDNotClonedTrend.setSelection(true);
		buttonSSIDNotClonedTrend.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		buttonSSIDNotClonedTrend.setBounds(214, 397, 13, 16);
		formToolkit.adapt(buttonSSIDNotClonedTrend, true, true);

		buttonGroupsNotClonedTrend = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.RIGHT);
		buttonGroupsNotClonedTrend.setSelection(true);
		buttonGroupsNotClonedTrend.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		buttonGroupsNotClonedTrend.setBounds(214, 421, 13, 16);
		formToolkit.adapt(buttonGroupsNotClonedTrend, true, true);

		final Label lblSsidNotCloned = formToolkit.createLabel(shell, "SSID not Cloned", SWT.NONE);
		lblSsidNotCloned.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblSsidNotCloned.setBounds(21, 402, 90, 15);



		Label lblGroupsNotCloned_1 = formToolkit.createLabel(shell, "Groups not Cloned", SWT.NONE);
		lblGroupsNotCloned_1.setToolTipText("Click me to find what groups");
		lblGroupsNotCloned_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				listGroupsNotCloned.setVisible(!listGroupsNotCloned.getVisible());
			}
		});
		lblGroupsNotCloned_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblGroupsNotCloned_1.setBounds(21, 423, 104, 15);


		Label lblLastUpdate_1 = formToolkit.createLabel(shell, "Last Update", SWT.NONE);
		lblLastUpdate_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblLastUpdate_1.setBounds(309, 400, 62, 15);

		Label lblThreadsRunning = formToolkit.createLabel(shell, "Threads Running", SWT.NONE);
		lblThreadsRunning.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblThreadsRunning.setBounds(21, 16, 90, 15);

		Label lblThreadsQueued = formToolkit.createLabel(shell, "Threads Queued", SWT.NONE);
		lblThreadsQueued.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblThreadsQueued.setBounds(21, 37, 87, 15);

		Label lblThreads_1 = formToolkit.createLabel(shell, "Threads Running", SWT.NONE);
		lblThreads_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblThreads_1.setBounds(21, 89, 90, 15);

		Label lblProgStarted = formToolkit.createLabel(shell, "Prog Started", SWT.NONE);
		lblProgStarted.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblProgStarted.setBounds(309, 421, 65, 15);

		Label lblLabel_ProgStarted = formToolkit.createLabel(shell, "New Label", SWT.RIGHT);
		lblLabel_ProgStarted.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblLabel_ProgStarted.setBounds(377, 423, 142, 15);

		lblLabel_ProgStarted.setText(Misc.getTimestamp());

		listGroupsNotCloned = new List(shell, SWT.BORDER);
		listGroupsNotCloned.setFont(SWTResourceManager.getFont("Courier New", 9, SWT.NORMAL));
		listGroupsNotCloned.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		listGroupsNotCloned.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		listGroupsNotCloned.setVisible(false);
		listGroupsNotCloned.setBounds(233, 16, 150, 361);
		formToolkit.adapt(listGroupsNotCloned, true, true);

		Label lblThreadsQueuedSize = formToolkit.createLabel(shell, "Total Queued", SWT.NONE);
		lblThreadsQueuedSize.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblThreadsQueuedSize.setBounds(266, 37, 72, 15);

		tableQueued = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		tableQueued.setFont(SWTResourceManager.getFont("Segoe UI", 8, SWT.NORMAL));
		tableQueued.setBounds(299, 110, 205, 251);
		formToolkit.adapt(tableQueued);
		formToolkit.paintBordersFor(tableQueued);
		tableQueued.setHeaderVisible(true);
		tableQueued.setLinesVisible(true);


		tblclmnNewColumn = new org.eclipse.swt.widgets.TableColumn(tableQueued, SWT.NONE);
		tblclmnNewColumn.setWidth(125);
		tblclmnNewColumn.setText("Clone");


		tblclmnSize = new org.eclipse.swt.widgets.TableColumn(tableQueued, SWT.NONE);
		tblclmnSize.setWidth(75);
		tblclmnSize.setText("Size");

		label_5 = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL);
		label_5.setBounds(255, 99, 2, 236);
		formToolkit.adapt(label_5, true, true);

		label_7 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_7.setBounds(10, 367, 500, 2);
		formToolkit.adapt(label_7, true, true);

		Label lblSize_1 = formToolkit.createLabel(shell, "Threads Queued", SWT.NONE);
		lblSize_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblSize_1.setBounds(299, 88, 90, 15);

		label_3 = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL);
		label_3.setBounds(258, 5, 2, 64);
		formToolkit.adapt(label_3, true, true);

		progressBarQueued = new ProgressBar(shell, SWT.NONE);
		progressBarQueued.setBounds(167, 37, 69, 17);
		formToolkit.adapt(progressBarQueued, true, true);

		progressBarRunning = new ProgressBar(shell, SWT.NONE);
		progressBarRunning.setBounds(167, 17, 69, 17);
		formToolkit.adapt(progressBarRunning, true, true);

		threadQueuedSize = new Text(shell, SWT.BORDER | SWT.RIGHT);
		threadQueuedSize.setFont(SWTResourceManager.getFont("Segoe UI", 8, SWT.NORMAL));
		threadQueuedSize.setBounds(354, 37, 76, 18);
		formToolkit.adapt(threadQueuedSize, true, true);

		threadRunningSize = new Text(shell, SWT.BORDER | SWT.RIGHT);
		threadRunningSize.setFont(SWTResourceManager.getFont("Segoe UI", 8, SWT.NORMAL));
		threadRunningSize.setBounds(354, 16, 76, 18);
		formToolkit.adapt(threadRunningSize, true, true);

		Label lblThreadsRunningSize = formToolkit.createLabel(shell, "Total Running", SWT.NONE);
		lblThreadsRunningSize.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblThreadsRunningSize.setBounds(266, 19, 75, 15);

		buttonThreadRunningTrend = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.RIGHT);
		buttonThreadRunningTrend.setSelection(true);
		//buttonThreadRunningTrend.setAlignment(SWT.DOWN);
		buttonThreadRunningTrend.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		buttonThreadRunningTrend.setBounds(238, 18, 13, 16);
		formToolkit.adapt(buttonThreadRunningTrend, true, true);

		buttonThreadQueuedTrend = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.RIGHT);
		buttonThreadQueuedTrend.setSelection(true);
		buttonThreadQueuedTrend.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		buttonThreadQueuedTrend.setBounds(238, 37, 13, 16);
		formToolkit.adapt(buttonThreadQueuedTrend, true, true);

		Label lblNumberClones = formToolkit.createLabel(shell, "Num of Running Clones", SWT.NONE);
		lblNumberClones.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblNumberClones.setBounds(21, 376, 128, 15);

		txtNumOfClones = formToolkit.createText(shell, "New Text", SWT.BORDER);
		txtNumOfClones.setText("0");
		txtNumOfClones.setBounds(159, 373, 49, 21);

		Button buttonClonesRunningTrend = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.RIGHT);
		buttonClonesRunningTrend.setSelection(true);
		buttonClonesRunningTrend.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		buttonClonesRunningTrend.setBounds(214, 375, 13, 16);
		formToolkit.adapt(buttonClonesRunningTrend, true, true);
		
		
		buttonSSIDNotClonedSizeTrend = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.RIGHT);
		buttonSSIDNotClonedSizeTrend.setSelection(true);
		buttonSSIDNotClonedSizeTrend.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		buttonSSIDNotClonedSizeTrend.setBounds(214, 443, 13, 16);
		formToolkit.adapt(buttonSSIDNotClonedSizeTrend, true, true);
		

		StatusThreadRunning = new Status(buttonThreadRunningTrend,threadRunning,lblThreadsRunning,progressBarRunning);
		StatusThreadRunning.setModifiedColor(SWTResourceManager.getColor(SWT.COLOR_CYAN));

		StatusQueuedRunning = new Status(buttonThreadQueuedTrend,threadQueued,lblThreadsQueued,progressBarQueued);
		StatusQueuedRunning.setModifiedColor(SWTResourceManager.getColor(SWT.COLOR_CYAN));

		StatusThreadRunningSize = new Status(buttonThreadsRunningSizeTrend,threadRunningSize,lblThreadsRunningSize,progressBarThreadsRunningSize);
		StatusThreadRunningSize.setModifiedColor(SWTResourceManager.getColor(SWT.COLOR_CYAN));

		StatusQueuedRunningSize = new Status(buttonThreadsQueuedSizeTrend,threadQueuedSize,lblThreadsQueuedSize,progressBarThreadsQueuedSize);
		StatusQueuedRunningSize.setModifiedColor(SWTResourceManager.getColor(SWT.COLOR_CYAN));

		StatusSSIDNotCloned = new StatusSmall(textSSIDNotCloned,lblSsidNotCloned,buttonSSIDNotClonedTrend);
		StatusSSIDNotCloned.setModifiedColor(SWTResourceManager.getColor(SWT.COLOR_CYAN));

		StatusGroupNotCloned = new StatusSmall(textGroupNotCloned,lblGroupsNotCloned_1,buttonGroupsNotClonedTrend);
		StatusGroupNotCloned.setModifiedColor(SWTResourceManager.getColor(SWT.COLOR_CYAN));

		StatusClonesRunning = new StatusSmall(txtNumOfClones,lblNumberClones,buttonClonesRunningTrend);
		StatusClonesRunning.setModifiedColor(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		
		StatusSSIDNotClonedSize = new StatusSmall(textSSIDNotClonedSize,lblSsidNotClonedSize,buttonSSIDNotClonedSizeTrend);
		StatusSSIDNotClonedSize.setModifiedColor(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		
		
		Label lblNewLabel_1 = formToolkit.createLabel(shell, "Session Info", SWT.NONE);
		lblNewLabel_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblNewLabel_1.setBounds(299, 379, 63, 15);
		//lblNewLabel_1.setToolTipText(Misc.getLocalHostIPaddress() + ":" + javaPID);
		
		Label lblSessionInfo = formToolkit.createLabel(shell, "session info", SWT.NONE);
		lblSessionInfo.setAlignment(SWT.RIGHT);
		//lblSessionInfo.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.NORMAL));
		lblSessionInfo.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblSessionInfo.setBounds(337, 444, 182, 15);
		
		lblSessionInfo.setText(Misc.getLocalHostIPaddress());
		
		Label lblConn = formToolkit.createLabel(shell, "Conn", SWT.NONE);
		lblConn.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblConn.setBounds(309, 442, 36, 15);
		
		Label lblJavaPid = formToolkit.createLabel(shell, "Java PID", SWT.NONE);
		lblJavaPid.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblJavaPid.setBounds(309, 465, 55, 15);
		
		String javaPID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
		Label lblJavaProcess = formToolkit.createLabel(shell, javaPID, SWT.NONE);
		lblJavaProcess.setAlignment(SWT.RIGHT);
		lblJavaProcess.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblJavaProcess.setBounds(419, 465, 100, 15);
								
										label_1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
										label_1.setAlignment(SWT.CENTER);
										label_1.setBounds(0, 75, 515, 7);
										formToolkit.adapt(label_1, true, true);
										
												tableRunning = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
												tableRunning.setLinesVisible(true);
												tableRunning.setHeaderVisible(true);
												tableRunning.setFont(SWTResourceManager.getFont("Segoe UI", 8, SWT.NORMAL));
												tableRunning.setBounds(21, 110, 204, 251);
												formToolkit.adapt(tableRunning);
												formToolkit.paintBordersFor(tableRunning);
												
														tableColumn = new org.eclipse.swt.widgets.TableColumn(tableRunning, SWT.NONE);
														tableColumn.setWidth(125);
														tableColumn.setText("Clone");
														
																tableColumn_1 = new org.eclipse.swt.widgets.TableColumn(tableRunning, SWT.NONE);
																tableColumn_1.setWidth(75);
																tableColumn_1.setText("Size");
		
		listSessions = new List(shell, SWT.BORDER);
		
		userName =  System.getProperty("user.name");
		
		
		listSessions.setBounds(21, 486, 317, 129);
		formToolkit.adapt(listSessions, true, true);
		StatusClonesRunning.setModifiedColor(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		listSessions.setVisible(false);
		
		Menu menu_1 = new Menu(shell);
		shell.setMenu(menu_1);
		
		MenuItem mntmRefreshTime = new MenuItem(menu_1, SWT.NONE);
		mntmRefreshTime.setText("Refresh Time");
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		final MenuItem mntmTest = new MenuItem(menu, SWT.CASCADE);
		mntmTest.setText("Refresh Time - 10 secs");
		
		Menu refreshTime = new Menu(mntmTest);
		
		mntmTest.setMenu(refreshTime);
		
		mntm10Secs = new MenuItem(refreshTime, SWT.RADIO);
		mntm10Secs.setText("10 secs (default)");
		mntm10Secs.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (mntm10Secs.getSelection()) {
					mntmTest.setText("Refresh Time - 10 secs");
					display.timerExec(refreshVal10secs, timer);;
				}
			}
		});
		mntm10Secs.setText("10 secs (default)");
		
		final MenuItem mntm30Secs = new MenuItem(refreshTime, SWT.RADIO);
		mntm30Secs.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (mntm30Secs.getSelection()) {
					mntmTest.setText("Refresh Time - 30 secs");
					display.timerExec(refreshVal30secs, timer);;
				}
			}
		});
		mntm30Secs.setText("30 secs");
		
		final MenuItem mntm1Min = new MenuItem(refreshTime, SWT.RADIO);
		mntm1Min.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (mntm1Min.getSelection()) {
					mntmTest.setText("Refresh Time - 1 min");
					display.timerExec(refreshVal1min, timer);
				}
			}
		});
		mntm1Min.setText("1 minute");
		
		final MenuItem mntm5Min = new MenuItem(refreshTime, SWT.RADIO);
		mntm5Min.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (mntm5Min.getSelection()) {
					mntmTest.setText("Refresh Time - 5 min");
					display.timerExec(refreshVal5min, timer);;
				}
			}
		});
		mntm5Min.setText("5 minute");
		
		final MenuItem mntm10Min = new MenuItem(refreshTime, SWT.RADIO);
		mntm10Min.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (mntm10Min.getSelection()) {
					mntmTest.setText("Refresh Time - 10 min");
					display.timerExec(refreshVal10min, timer);;
				}
			}
		});
		mntm10Min.setText("10 minute");
		
		refreshTime.setDefaultItem(mntm10Secs);
		mntm10Secs.setSelection(true);
		
		mntm30Min = new MenuItem(refreshTime, SWT.RADIO);
		mntm30Min.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (mntm30Min.getSelection()) {
					mntmTest.setText("Refresh Time - 30 min");
					display.timerExec(refreshVal30min, timer);
				}
			}
		});
		mntm30Min.setText("30 minute");
		
		sessionInfoString = userName + ":" + Misc.getLocalHostIPaddress() + ":" + javaPID;

		updateFields();	
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
		
		progressBarThreadsRunningSize.setMaximum(10000000);
		progressBarThreadsQueuedSize.setMaximum(10000000);
		

		

	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.addHelpListener(new HelpListener() {
			public void helpRequested(HelpEvent arg0) {
				//System.out.println("help");
				
				
				if (f1pressed) {
					Set<String> s = jdw.keys("temp:cloneStats:session*");
					listSessions.removeAll();
					for (String sess : s) {
						listSessions.add(sess.split("_")[1]);
					}
					shell.setSize(545, 680);
					listSessions.setVisible(f1pressed);
					f1pressed = false;
					
				} else {
				
				listSessions.setVisible(f1pressed);
				f1pressed = true;
				shell.setSize(545, 539);
				}
			}
		});
		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {
				jdw.del("temp:cloneStats:sessions" + "_" + sessionInfoString);
			}
		});
		shell.addShellListener(new ShellAdapter() {
			@Override
			public void shellIconified(ShellEvent e) {
				iconnized = true;
			}
			@Override
			public void shellDeiconified(ShellEvent e) {
				iconnized = false;
				updateFields();
			}
			@Override
			public void shellClosed(ShellEvent e) {
				jdw.del("temp:cloneStats:sessions" + "_" + sessionInfoString);
				
			}
		});


		shell.setImage(SWTResourceManager.getImage(cloneStats.class, "/com/herohead/icons/red-icon-for-social-network-300x300.png"));
		shell.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				listGroupsNotCloned.setVisible(false);
			}
		});
		shell.setModified(true);
		shell.setSize(545, 539);
		shell.setText("Clone Status : " + version);
		shell.setLayout(null);
		
		lblSsidNotClonedSize = formToolkit.createLabel(shell, "SSID not Cloned Size", SWT.NONE);
		lblSsidNotClonedSize.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblSsidNotClonedSize.setBounds(21, 444, 128, 15);
		
		textSSIDNotClonedSize = formToolkit.createText(shell, "New Text", SWT.NONE);
		textSSIDNotClonedSize.setText("0");
		textSSIDNotClonedSize.setBounds(143, 441, 65, 21);

		threadRunning = new Text(shell, SWT.BORDER);
		threadRunning.setBackground(SWTResourceManager.getColor(255, 255, 255));
		threadRunning.setBounds(125, 16, 36, 18);
	}

// update fields - uf
	
	public static  void updateFields() {

		if (iconnized) {return;}

		Misc m = new Misc();
		
		jdw.setex("temp:cloneStats:sessions" + "_" + sessionInfoString, 360,"");
		
		
		//System.out.println(m.bytesToMB(jd.get(RedisKeys.strProdSizeNotCloned)));

		StatusThreadRunning.checkIfChanged(1,jd.llen(RedisKeys.listProdThreadRunning));
		StatusQueuedRunning.checkIfChanged(2,jd.hlen(RedisKeys.hashProdThreadQueuedProcessID));
		StatusThreadRunningSize.checkIfChanged(3,Double.parseDouble(getSize(RedisKeys.hashProdThreadRunningProcessID)));
		StatusQueuedRunningSize.checkIfChanged(4,Double.parseDouble(getSize(RedisKeys.hashProdThreadQueuedProcessID)));
		StatusSSIDNotCloned.checkIfChanged(5,jd.scard(RedisKeys.setProdSSIDNotCloned));
		StatusGroupNotCloned.checkIfChanged(6,jd.scard(RedisKeys.setProdGroupNotCloned));
		StatusClonesRunning.checkIfChanged(7, jd.llen(RedisKeys.listProdCloneRunning));
		StatusSSIDNotClonedSize.checkIfChanged(8, Double.parseDouble(m.bytesToMB(jd.get(RedisKeys.strProdSizeNotCloned))));

		Set<Tuple> runningList = jd.zrevrangeWithScores("z_prod:clone:running:size", 0, -1);
		Map <String, String> temp1 = new HashMap<String, String>();
		temp1 = copyTable(tableRunning);
		tableRunning.removeAll();
		compareLists(temp1, runningList,tableRunning);
		
		Set<Tuple> queuedList = jd.zrevrangeWithScores("z_prod:clone:queued:size", 0, -1);
		//temp1 = new HashMap<String, String>();
		temp1 = copyTable(tableQueued);
		tableQueued.removeAll();
		compareLists(temp1, queuedList,tableQueued);
		
		listGroupsNotCloned.removeAll();
		listGroupsNotCloned.add("Groups not Cloned");
		listGroupsNotCloned.add("-----------------");

		Set<String> s = jd.smembers(RedisKeys.setProdGroupNotCloned);
		for (String ss : s){
			listGroupsNotCloned.add(ss);
		}
		lastUpdate.setText(Misc.getTimestamp());
	}

	public static Map <String, String> copyTable(Table temp) {

		//Set<List> setList;
		//static Map <Integer, Double> lastValues1 = new HashMap<Integer, Double>();
		Map <String, String> temp1 = new HashMap<String, String>();
		for (int i=0;i < temp.getItemCount();i++) {
			temp1.put(temp.getItem(i).getText(0), temp.getItem(i).getText(1));
			//System.out.println("Adding to MAP " + temp.getItem(i).getText(0) + " " + temp.getItem(i).getText(1));
		}
		return temp1;
	}

	public static void compareLists(Map <String, String> tempTab, Set<Tuple> newData,Table newTable) {
		//System.out.println("Table count " +tempTab.size());
		//System.out.println("RedisList " + newData.size());
		for (Tuple d : newData) {
			//System.out.println(d.getElement());
			//System.out.println(String.format("%.0f", d.getScore()/1024/1204));
			TableItem item = new TableItem(newTable, SWT.NULL);
			if (compareTableData(tempTab, d.getElement(), String.format("%.0f", d.getScore()/1024/1204))) {
				if (d.getScore()/1024/1204 > 0 ) { 
					item.setBackground((SWTResourceManager.getColor(SWT.COLOR_WHITE)));
				
				item.setText(0, d.getElement());
				item.setText(1, String.format("%.0f", d.getScore()/1024/1204));
				}
			} else {
				if (d.getScore()/1024/1204 > 0 ) {
					item.setBackground((SWTResourceManager.getColor(SWT.COLOR_CYAN)));
				
				item.setText(0, d.getElement());
				item.setText(1, String.format("%.0f", d.getScore()/1024/1204));
				}
			}
		}	
	}

	public static boolean compareTableData(Map <String, String>  tempTab, String cloneName, String size ) {
		boolean match = false;

		//System.out.println(cloneName);
		//System.out.println(size);
		
		if (tempTab.containsKey(cloneName)) {
			
			String s = tempTab.get(cloneName);
			if (s.contains(size)) {
				//System.out.println("Found match");
				match = true;
			}
		}
		return match;
	}



	@SuppressWarnings("rawtypes")
	public static String getSize (String hash) {

		long size = 0;

		Map<String, String> mp = jd.hgetAll(hash);

		Iterator it = mp.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry pairs = (Map.Entry)it.next();
			//System.out.println(pairs.getKey() + " = " + pairs.getValue());
			String temp = pairs.getValue().toString();
			size = size + Long.parseLong(temp);
		}

		Misc m = new Misc();

		String s = m.bytesToMB(String.valueOf(size));

		return s;
	}





	public static boolean checkIfTableChanged(int indexNumber ,double value, Table trend) {

		double lastV;
		boolean updated = false;


		//double passV = Double.parseDouble(value);

		if (lastValues.containsKey(indexNumber)) {
			lastV = lastValues.get(indexNumber);
			lastValues.put(indexNumber, value);
			if (lastV != value) {
				updated=true;

			}
		} else {
			lastValues.put(indexNumber, value);
			updated = true;
		}

		if (updated) {
			trend.setToolTipText("Last Change = " + Misc.getTimestamp());
			trend.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		} else {
			trend.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		}


		return updated;

	}


	public static boolean checkIfChanged(int indexNumber ,double value, Button trend) {

		double lastV;
		boolean updated = false;


		//double passV = Double.parseDouble(value);

		if (lastValues.containsKey(indexNumber)) {
			lastV = lastValues.get(indexNumber);
			lastValues.put(indexNumber, value);
			if (lastV != value) {
				updated=true;

			}
		} else {
			lastValues.put(indexNumber, value);
			updated = true;
		}

		if (updated) {
			trend.setToolTipText("Last Change = " + Misc.getTimestamp());
			if (trend.getAlignment() == SWT.UP) {
				trend.setAlignment(SWT.DOWN);
			} else {
				trend.setAlignment(SWT.UP);
			}
		}


		return updated;

	}
	public static String formatTabSpacing(String f, String l) {

		if (f.length() < 6) {
			return f + "                  " + l;
		} else if (f.length() < 16) {
			return f + "            " + l;
		} else {
			return f + "      " + l;
		}

	}
}


class Status {
	public Button button;
	String textBoxText;
	Text textBox;
	Label lbl;
	String lblText;
	ProgressBar progressBar;
	Color bColor;
	Color modifiedColor;
	static Map <Integer, Double> lastValues = new HashMap<Integer, Double>();

	public int backgroundColor;
	//constructor
	public Status(Button trend,Text tText,Label Llbl,ProgressBar proBar) {
		this.button = trend;
		this.textBox = tText;
		this.lbl = Llbl;
		this.progressBar = proBar;
		this.bColor = textBox.getBackground();
	}

	public void setTextBoxText(String boxText) {
		this.textBoxText = boxText;
		textBox.setText(boxText);
	}

	public String getTextBoxText() {
		return this.textBoxText;

	}
	public void setLabelText(String slblText) {
		this.lblText = slblText;
	}

	public void setBackground(int backColor) {
		backgroundColor = backColor;
	}

	public Button getButton() {
		return this.button;
	}

	public void resetState() {
		textBox.setBackground(bColor);
	}

	public void setModifiedColor(Color mc) {
		modifiedColor = mc;
	}

	public void modifiedField() {
		textBox.setBackground(modifiedColor);

	}

	public static String fmt(double d)
	{
		if(d == (int) d)
			return String.format("%d",(int)d);
		else
			return String.format("%s",d);
	}

	public void checkIfChanged(int indexNumber ,double value) {

		double lastV;
		boolean updated = false;

		textBox.setText(fmt(value));
		//setTextBoxText = value;

		if (lastValues.containsKey(indexNumber)) {
			lastV = lastValues.get(indexNumber);
			lastValues.put(indexNumber, value);
			if (lastV != value) {
				updated=true;
			}
		} else {
			lastValues.put(indexNumber, value);
			updated = true;
		}

		if (updated) {
			button.setToolTipText("Last Change = " + Misc.getTimestamp());
			this.modifiedField();
			progressBar.setSelection((int) value);
			progressBar.setToolTipText(fmt(value));

			if (button.getAlignment() == SWT.UP) {
				button.setAlignment(SWT.DOWN);
			} else {
				button.setAlignment(SWT.UP);

			}

		} else {
			this.resetState();
		}


	}
}

class StatusSmall {
	public Button button;
	String textBoxText;
	Text textBox;
	Label lbl;
	String lblText;
	ProgressBar progressBar;
	Color bColor;
	Color modifiedColor;
	static Map <Integer, Double> lastValues = new HashMap<Integer, Double>();

	public int backgroundColor;
	//constructor
	public StatusSmall(Text tText,Label Llbl,Button b) {

		this.textBox = tText;
		this.lbl = Llbl;
		this.bColor = textBox.getBackground();
		this.button = b;
	}

	public void setTextBoxText(String boxText) {
		this.textBoxText = boxText;
		textBox.setText(boxText);
	}

	public String getTextBoxText() {
		return this.textBoxText;

	}
	public void setLabelText(String slblText) {
		this.lblText = slblText;
	}

	public void setBackground(int backColor) {
		backgroundColor = backColor;
	}


	public void resetState() {
		textBox.setBackground(bColor);
	}

	public void setModifiedColor(Color mc) {
		modifiedColor = mc;
	}

	public void modifiedField() {
		textBox.setBackground(modifiedColor);
		//textBox.setText(text);
	}

	public static String fmt(double d)
	{
		if(d == (int) d)
			return String.format("%d",(int)d);
		else
			return String.format("%s",d);
	}

	public void checkIfChanged(int indexNumber ,double value) {

		double lastV;
		boolean updated = false;

		textBox.setText(fmt(value));
		//setTextBoxText = value;

		if (lastValues.containsKey(indexNumber)) {
			lastV = lastValues.get(indexNumber);
			lastValues.put(indexNumber, value);
			if (lastV != value) {
				updated=true;

			}
		} else {
			lastValues.put(indexNumber, value);
			updated = true;
		}

		if (updated) {
			button.setToolTipText("Last Change = " + Misc.getTimestamp());
			this.modifiedField();

			if (button.getAlignment() == SWT.UP) {
				button.setAlignment(SWT.DOWN);

			} else {
				button.setAlignment(SWT.UP);

			}
		} else {
			this.resetState();
		}


		//return updated;

	}
}

